package com.space.generator.biz;

import com.space.generator.AutoGenerator;
import com.space.generator.InjectionConfig;
import com.space.generator.config.*;
import com.space.generator.config.po.TableInfo;
import com.space.generator.config.rules.DbType;
import com.space.generator.config.rules.NamingStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @标题：
 * @描述：
 * @包名：com.space.generator.biz
 * @项目名：plus-generator
 * @公司：
 * @创建者：crit
 * @修改者：crit
 * @创建日期： 2018-12-11 15:25
 * @修改日期： 2018-12-11 15:25
 * @版本：1.0
 */
@Configuration
public class GeneratorBiz {

    @Value("${crit.datasource.url}")
    private String url;
    @Value("${crit.datasource.username}")
    private String username;
    @Value("${crit.datasource.password}")
    private String password;
    @Value("${crit.datasource.driverName}")
    private String driverName;

    @Value("${crit.tablePrefix}")
    private String tablePrefix;

    @Value("${crit.commonEntity}")
    private String commonEntity;
    @Value("${crit.commonMapper}")
    private String commonMapper;
    @Value("${crit.commonService}")
    private String commonService;
    @Value("${crit.commonServiceImpl}")
    private String commonServiceImpl;
    @Value("${crit.outPath}")
    private String outPath;

    @Value("${crit.author}")
    private String author;

    @Value("${crit.packageName}")
    private String packageName;
    @Value("${crit.tableNames}")
    private String[] tableNames;
    @Value("${crit.projectName}")
    private String projectName;
    @Value("${crit.platform}")
    private String platform;


    public void generateCode() {


        boolean serviceNameStartWithI = true;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, tableNames);

//        generateByTables(serviceNameStartWithI, packageName, "crit_riskmac_user");

    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String[] tableNames) {
        GlobalConfig config = new GlobalConfig();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.ORACLE)
                .setUrl(url)
                .setUsername(username)
                .setPassword(password)
                .setDriverName(driverName);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true) //引用lombok
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setRestControllerStyle(true)
                .setTablePrefix(tablePrefix)
                .setSuperEntityClass(commonEntity)
                .setSuperMapperClass(commonMapper)
                .setSuperServiceClass(commonService)
                .setSuperServiceImplClass(commonServiceImpl)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组

        config.setActiveRecord(true)
                .setAuthor(author)
                // .setOutputDir("/users/space/codeGen")
                .setOutputDir(outPath)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setProjectName(projectName)
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }

        // 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                //  map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        // 自定义 xxList.jsp 生成
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
        // 调整 xml 生成目录演示
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String path =outPath +"\\mapper\\";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                return path + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setCfg(cfg)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")


                                .setEntity("entity")
                ).execute();
    }

//    private void generateByTables(String packageName, String... tableNames) {
//        generateByTables(true, packageName, tableNames);
//    }
}

